package com.jishuo.nontax.bfc.service;

import com.github.pagehelper.PageHelper;
import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.bean.Pagination;
import com.jishuo.base.service.SequenceService;
import com.jishuo.nontax.bean.vo.BankVO;
import com.jishuo.nontax.bean.vo.WaitaffirmPayInfoVO;
import com.jishuo.nontax.bfc.dao.BankMapper;
import com.jishuo.nontax.bfc.dao.WaitaffirmPayInfoMapper;
import com.jishuo.nontax.bfc.pzk.PzkService;
import com.jishuo.nontax.bfc.tlq.MessageFactory;
import com.jishuo.nontax.bfc.tlq.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hlp
 * @date 2023/2/2 10:42
 * @Description: 99支出 业务层
 */
@Service
public class WaitaffirmPayInfoService {
    private static Logger logger = LoggerFactory.getLogger(WaitaffirmPayInfoService.class);
    @Resource
    private WaitaffirmPayInfoMapper waitaffirmPayInfoMapper;

    @Resource
    private BankMapper bankMapper;
    @Resource
    private SequenceService sequenceService;
    @Resource
    private MessageService messageService;
    @Resource
    private PzkService pzkService;


    /**
     * @author hlp
     * @date 2023/2/2 08:58
     * @Description: 新增修改 99支出 , api
     */
    public BaseResult setWaitaffirmPayInfo(WaitaffirmPayInfoVO waitaffiroVO) {
        try {
            //根据 银行编号查出 银行唯一标识号
            waitaffiroVO.setBankid(bankMapper.findBankId(waitaffiroVO.getAccountcode()));
            waitaffiroVO.setPaybankdtSub(waitaffiroVO.getPaybankdt().substring(0, 10));
            /*
             * 查询当日是否有支出记录，进行相应新增和修改操作
             */
            WaitaffirmPayInfoVO fvo = waitaffirmPayInfoMapper.selecWaitaffirmpayinfo(waitaffiroVO.getBankaffirm(), waitaffiroVO.getBankid(), waitaffiroVO.getPaybankdt().substring(0, 10));
            if (fvo != null) {
                if (waitaffirmPayInfoMapper.updateWaitaffirmpayinfo(waitaffiroVO) > 0) {
                    return new BaseResult();
                }
            } else {
                long waitaffirmpayid = sequenceService.getNextSeq("FSWAITAFFIRMPAYINFOSEQ");
                waitaffiroVO.setWaitaffirmpayid(waitaffirmpayid);
                if (waitaffirmPayInfoMapper.insertWaitaffirmpayinfo(waitaffiroVO) > 0) {
                    return new BaseResult();
                }
            }
            return new BaseResult(ErrorCodeEnum.DB_ERR);
        } catch (Exception e) {
            logger.info("处理待查支出失败：" + e.toString());
            return new BaseResult(ErrorCodeEnum.UNKNOWN_ERR);
        }
    }

    /**
     * @author hlp
     * @date 2023/2/2 10:41
     * @Description: 查询99支出信息 发送到财政
     */
    public BaseResult sendWaitAffirmPayInfo(Long bankid, String accountcode, String paybankdt) {
        try {
            if (bankid == null) {
                BankVO bank = bankMapper.findBankVO(accountcode);
                bankid = bank.getBankid();
            }
            //根据银行唯一标识号和时间，查询支出信息
            List<WaitaffirmPayInfoVO> list = waitaffirmPayInfoMapper.selectWaitaffirPayInfo(bankid, paybankdt);
            for (WaitaffirmPayInfoVO vo : list) {
                this.sendWaitaffirmPayInfo(vo);
            }
            return new BaseResult();
        } catch (Exception e) {
            logger.info("处理失败：" + e.toString());
            return new BaseResult(ErrorCodeEnum.UNKNOWN_ERR);
        }
    }

    // 页面

    /**
     * 99支出列表
     *
     * @param waitaffirmPayInfoVO
     * @param pagination
     * @return
     */
    public List<WaitaffirmPayInfoVO> findWaitaffirmPayInfoPage(WaitaffirmPayInfoVO waitaffirmPayInfoVO, Pagination pagination) {
        if (pagination.getRows() > 0) PageHelper.startPage(pagination.getPage(), pagination.getRows());

        return waitaffirmPayInfoMapper.findWaitaffirmPayInfoPage(waitaffirmPayInfoVO);
    }

    /**
     * 修改99支出信息，web
     *
     * @param vo
     * @return
     */
    public BaseResult saveWaitaffirmPayInfo(WaitaffirmPayInfoVO vo) {
        int i = waitaffirmPayInfoMapper.saveWaitaffirmPayInfo(vo);
        if (i < 1) return new BaseResult(ErrorCodeEnum.DB_ERR);
        return new BaseResult();

    }

    /**
     * 添加99支出信息，web
     *
     * @param vo
     * @return
     */
    public BaseResult insertWaitaffirmPayInfo(WaitaffirmPayInfoVO vo) {
        WaitaffirmPayInfoVO fvo = waitaffirmPayInfoMapper.selecWaitaffirmpayinfo(vo.getBankaffirm(), vo.getBankid(), vo.getPaybankdt().substring(0, 10));
        if (fvo != null) return new BaseResult(ErrorCodeEnum.CUSTOM_ERR.getCode(), "此交易流水已存在");
        try {
            vo.setWaitaffirmpayid(sequenceService.getNextSeq("FSWAITAFFIRMPAYINFOSEQ"));
            if (waitaffirmPayInfoMapper.insertWaitaffirmpayinfo(vo) > 0) {
                return new BaseResult();
            } else {
                return new BaseResult(ErrorCodeEnum.DB_ERR);
            }
        } catch (Exception e) {
            logger.info("保存99错误。", e);
            return new BaseResult(ErrorCodeEnum.SYS_ERR.getCode(), e.getMessage());
        }
    }

    /**
     * 根据id 删除99支出信息，web
     *
     * @param
     * @return
     */
    public BaseResult deleteWaitaffirmPayInfo(String waitaffirmpayids) {
        String[] ids = waitaffirmpayids.split(",");
        if (waitaffirmPayInfoMapper.deleteWaitaffirmPayInfo(ids) > 0) {
            return new BaseResult();
        }
        return new BaseResult(ErrorCodeEnum.DB_ERR);
    }


    /**
     * 发送99，web
     * @param billids id列表
     * @return
     */
    public BaseResult senfWaitaffirmPayInfo(String billids) {
        String[] ids = billids.split(",");
        List<WaitaffirmPayInfoVO> list = waitaffirmPayInfoMapper.findWaitaffirmPayInfo(ids);

        for (WaitaffirmPayInfoVO vo : list) {
            this.sendWaitaffirmPayInfo(vo);
        }
        return new BaseResult();
    }

    /**
     * 发送消息
     * @param vo
     * @return
     */
    private BaseResult sendWaitaffirmPayInfo(WaitaffirmPayInfoVO vo) {
        try {
            Object obj = MessageFactory.setWaitaffirmPayInfoDTO(vo);
            long messageid = sequenceService.getNextSeq("ELEMESSAGESEQ");
            messageService.sendMessage("SETWAITAFFIRMPAYINFO", messageid, obj, "2.4");
            // 写凭证
            pzkService.savePzmsg("SETWAITAFFIRMPAYINFOPZK", vo);
        } catch (Exception e) {
            logger.info("学生缴费失败：" + e.toString());
            return new BaseResult(ErrorCodeEnum.TLQ_ERR);
        }

        return new BaseResult();
    }


    public BaseResult getWaitaffirmPayIngo(Long waitaffirmpayid) {

        return new BaseResult(waitaffirmPayInfoMapper.getWaitaffirmPayInfo(waitaffirmpayid));
    }

    public BaseResult findColum() {
        List<WaitaffirmPayInfoVO> colum = waitaffirmPayInfoMapper.findColum();
        return new BaseResult(colum);
    }

    //其他支出操作
    public BaseResult setWaitaffirmPayInfo1(WaitaffirmPayInfoVO vo) {
        int i = 0;
        vo.setPayeeaccountnum(vo.getPayeraccount());
        vo.setPayeeaccountname(vo.getPayer());
        if (vo.getWaitaffirmpayid() != null && vo.getWaitaffirmpayid() != 0) {
            i = waitaffirmPayInfoMapper.saveWaitaffirmPayInfo(vo);
        } else {
            i = (waitaffirmPayInfoMapper.insertWaitaffirmpayinfo(vo));
        }
        if (i < 0) return new BaseResult(ErrorCodeEnum.DB_ERR);
        return new BaseResult();
    }

}
